package com.zhaodao.dubbo.demo.consumer.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.rpc.RpcContext;
import com.zhaodao.dubbo.demo.DemoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

@RestController
@RequestMapping(value = {"/test"})
@Api(tags = {"测试"})
public class DemoConsumerController {
    // 异步调用
   // @Reference(interfaceClass = DemoService.class,interfaceName = "sayHello",async = true)
    @Reference
    private DemoService demoService;

    @PostMapping(value = "/sayHello")
    @ApiOperation(value = "测试请求")
    @ApiImplicitParam(paramType = "header",name = "Authorization",value = "身份认证Token")
    public String sayHello( @RequestBody @ApiParam("资源数据") String name) throws ExecutionException, InterruptedException {
        demoService.sayHello(name);
        String remoteHost = RpcContext.getContext().getRemoteHost();
        String application = RpcContext.getContext().getUrl().getParameter("application");
        System.out.println(remoteHost);
        System.out.println(application);
        Future<String> future = RpcContext.getContext().getFuture();
        String str = future.get();
        return str;
    }

}